package com.ruoyi.collegeClass.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.collegeClass.domain.BwClass;
import com.ruoyi.collegeClass.domain.vo.BwStudentVo;
import com.ruoyi.collegeClass.domain.vo.SaveVo;
import com.ruoyi.collegeClass.domain.vo.SysUserRoleVo;
import com.ruoyi.common.core.constant.TokenConstants;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.MyMarkAnnotation;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.model.LoginUser;
import io.jsonwebtoken.Claims;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.collegeClass.domain.BwStudent;
import com.ruoyi.collegeClass.service.IBwStudentService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.server.ServerWebExchange;

/**
 * 学生Controller
 * 
 * @author ruoyi
 * @date 2024-05-24
 */
@RestController
@RequestMapping("/student")
public class BwStudentController extends BaseController
{
    @Autowired
    private IBwStudentService bwStudentService;

    /**
     * 查询学生列表
     */
    @RequiresPermissions("collegeClass:student:list")
    @GetMapping("/list")
    public TableDataInfo list(BwStudent bwStudent)
    {
        startPage();
        List<BwStudent> list = bwStudentService.selectBwStudentList(bwStudent);
        List<BwStudentVo> bwStudents = new ArrayList<>();
        for (BwStudent student : list) {

            Long classId = student.getClassId();
            BwClass bwClass = bwStudentService.selectClassById(classId);
            if (bwClass!=null){
                student.setClassName(bwClass.getClassName());
            }

            Long collegeId = student.getCollegeId();

            SysDept sysDept = bwStudentService.selectDeptById(collegeId);
            if (sysDept!=null){
                student.setCollegeName(sysDept.getDeptName());
            }
            Long schoolId = student.getSchoolId();
            SysDept sysDept1 = bwStudentService.selectDeptById(schoolId);
            if (sysDept1!=null){
                student.setSchoolName(sysDept1.getDeptName());
            }

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String EnrollmentTime=null;
            String GraduationTime=null;
            String CreateTime=null;
            String UpdateTime=null;
            if (student.getEnrollmentTime()!=null){
                EnrollmentTime = dateFormat.format(student.getEnrollmentTime());
            }
            if (student.getGraduationTime()!=null){
                 GraduationTime = dateFormat.format(student.getGraduationTime());
            }
            if (student.getCreateTime()!=null){
                 CreateTime = dateFormat.format(student.getCreateTime());
            }
            if (student.getUpdateTime()!=null){
                 UpdateTime = dateFormat.format(student.getUpdateTime());
            }
            String o = MyMarkAnnotation.markDetail1(student);
            BwStudentVo bwStudentVo = JSON.parseObject(o, BwStudentVo.class);
            bwStudentVo.setCreateTime(CreateTime);
            bwStudentVo.setEnrollmentTime(EnrollmentTime);
            bwStudentVo.setGraduationTime(GraduationTime);
            bwStudentVo.setUpdateTime(UpdateTime);
            bwStudents.add(bwStudentVo);
        }
        return getDataTable(bwStudents);
    }

    /**
    * @Author: NanMu
    * @Description: 查询班级下的学生列表
    * @Param: [id]
    * @return: com.ruoyi.common.core.web.page.TableDataInfo
    * @Date: 2024/5/24 20:29
    */
//    @RequiresPermissions("collegeClass:student:list")
    @GetMapping("/listById/{id}")
    public TableDataInfo listById(
            @PathVariable(name = "id") Integer  id)
    {
        startPage();
        List<BwStudent> list = bwStudentService.listById(id);



        return getDataTable(list);
    }

    @RequiresPermissions("collegeClass:student:getStudentInfo")
    @GetMapping("getStudentInfo/{sid}")
    public BwStudent getStudentInfo(@PathVariable(name = "sid") Long sid)
    {
                BwStudent bwStudent = bwStudentService.getStudentInfo(sid);

                if (bwStudent!=null){
                    return bwStudent;
                }
                return new BwStudent();
    }

    @GetMapping("/listStu")
    public TableDataInfo listStu()
    {
        startPage();
        List<BwStudent> list = bwStudentService.listStu();
        return getDataTable(list);
    }

    /**
     * 导出学生列表
     */
    @RequiresPermissions("collegeClass:student:export")
    @Log(title = "学生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BwStudent bwStudent)
    {
        List<BwStudent> list = bwStudentService.selectBwStudentList(bwStudent);
        ExcelUtil<BwStudent> util = new ExcelUtil<BwStudent>(BwStudent.class);
        util.exportExcel(response, list, "学生数据");
    }

    /**
     * 获取学生详细信息
     */
//    @RequiresPermissions("collegeClass:student:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bwStudentService.selectBwStudentById(id));
    }

    /**
     * 新增学生
     */
    @RequiresPermissions("collegeClass:student:add")
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BwStudent bwStudent)
    {
        return toAjax(bwStudentService.insertBwStudent(bwStudent));
    }

//    @RequiresPermissions("collegeClass:student:add")
//    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping("stuSave")
    public AjaxResult stuSave(@RequestBody SaveVo saveVo)
    {
        return toAjax(bwStudentService.stuSave(saveVo));
    }

    /**
     * 修改学生
     */
    @RequiresPermissions("collegeClass:student:edit")
    @Log(title = "学生", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BwStudent bwStudent)
    {
        bwStudent.setUpdateTime(new Date());
        return toAjax(bwStudentService.updateBwStudent(bwStudent));
    }

    /**
     * 删除学生
     */
    @RequiresPermissions("collegeClass:student:remove")
    @Log(title = "学生", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bwStudentService.deleteBwStudentByIds(ids));
    }
}
